package top.szhome.gateway.util;

import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpCookie;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import top.szhome.framework.core.tools.util.JsonUtil;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashSet;

/**
 * Spring Gateway版本Web工具类
 *
 * @author 唐加利
 * @date 2023/8/17
 **/
public class WebUtil {

    /**
     * 获取源请求地址
     * @param exchange
     * @return
     */
    public static String getOriginalRequestUrl(ServerWebExchange exchange) {
        ServerHttpRequest request = exchange.getRequest();
        LinkedHashSet<URI> uris = exchange.getRequiredAttribute(ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
        URI requestUri = uris.stream().findFirst().orElse(request.getURI());
        MultiValueMap<String, String> queryParams = request.getQueryParams();
        return UriComponentsBuilder.fromPath(requestUri.getRawPath()).queryParams(queryParams).build().toUriString();
    }

    /**
     * 获取请求头
     * @param exchange
     * @param header
     * @return
     */
    public static String getHeader(ServerWebExchange exchange, String header) {
        return getRequest(exchange).getHeaders().getFirst(header);
    }

    /**
     * 获取请求参数
     * @param exchange
     * @param param
     * @return
     */
    public static String getParam(ServerWebExchange exchange, String param) {
        return getRequest(exchange).getQueryParams().getFirst(param);
    }

    /**
     *
     * @param exchange
     * @param name
     * @return
     */
    public static String getCookie(ServerWebExchange exchange, String name) {
        HttpCookie cookie = getRequest(exchange).getCookies().getFirst(name);
        if(cookie == null){
            return null;
        }

        return cookie.getValue();
    }

    /**
     * 响应数据
     * @param exchange
     * @param object
     * @return
     */
    public static Mono<Void> render(ServerWebExchange exchange, Object object) {
        ServerHttpResponse response = getResponse(exchange);
        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        String result = JsonUtil.toJson(object);
        DataBuffer buffer = response.bufferFactory().wrap(result.getBytes(StandardCharsets.UTF_8));
        return response.writeWith(Flux.just(buffer));
    }

    /**
     * 获取请求对象
     * @param exchange
     * @return
     */
    public static ServerHttpRequest getRequest(ServerWebExchange exchange){
        ServerHttpRequest request = exchange.getRequest();
        return request;
    }

    /**
     * 获取响应对象
     * @param exchange
     * @return
     */
    public static ServerHttpResponse getResponse(ServerWebExchange exchange){
        return exchange.getResponse();
    }

}
